package cn.iocoder.yudao.module.biz.controller.admin.staff;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.annotations.*;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog;
import static cn.iocoder.yudao.framework.operatelog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.biz.controller.admin.staff.vo.*;
import cn.iocoder.yudao.module.biz.dal.dataobject.staff.BizStaffDO;
import cn.iocoder.yudao.module.biz.convert.staff.BizStaffConvert;
import cn.iocoder.yudao.module.biz.service.staff.BizStaffService;

@Api(tags = "管理后台 - 员工")
@RestController
@RequestMapping("/biz/staff")
@Validated
public class BizStaffController {

    @Resource
    private BizStaffService staffService;

    @PostMapping("/create")
    @ApiOperation("创建员工")
    @PreAuthorize("@ss.hasPermission('biz:staff:create')")
    public CommonResult<String> createStaff(@Valid @RequestBody BizStaffCreateReqVO createReqVO) {
        return success(staffService.createStaff(createReqVO));
    }

    @PutMapping("/update")
    @ApiOperation("更新员工")
    @PreAuthorize("@ss.hasPermission('biz:staff:update')")
    public CommonResult<Boolean> updateStaff(@Valid @RequestBody BizStaffUpdateReqVO updateReqVO) {
        staffService.updateStaff(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @ApiOperation("删除员工")
    @ApiImplicitParam(name = "id", value = "编号", required = true, dataTypeClass = String.class)
    @PreAuthorize("@ss.hasPermission('biz:staff:delete')")
    public CommonResult<Boolean> deleteStaff(@RequestParam("id") String id) {
        staffService.deleteStaff(id);
        return success(true);
    }

    @GetMapping("/get")
    @ApiOperation("获得员工")
    @ApiImplicitParam(name = "id", value = "编号", required = true, example = "1024", dataTypeClass = String.class)
    @PreAuthorize("@ss.hasPermission('biz:staff:query')")
    public CommonResult<BizStaffRespVO> getStaff(@RequestParam("id") String id) {
        BizStaffDO staff = staffService.getStaff(id);
        return success(BizStaffConvert.INSTANCE.convert(staff));
    }

    @GetMapping("/getStaffByCode")
    @ApiOperation("获得员工")
    @ApiImplicitParam(name = "id", value = "编号", required = true, example = "1024", dataTypeClass = String.class)
    @PreAuthorize("@ss.hasPermission('biz:staff:query')")
    public CommonResult<BizStaffRespVO> getStaffByCode(@RequestParam("staffCode") String staffCode) {
        BizStaffDO staff = staffService.getStaffByCode(staffCode);
        return success(BizStaffConvert.INSTANCE.convert(staff));
    }

    @GetMapping("/list")
    @ApiOperation("获得员工列表")
    @ApiImplicitParam(name = "ids", value = "编号列表", required = true, example = "1024,2048", dataTypeClass = List.class)
    @PreAuthorize("@ss.hasPermission('biz:staff:query')")
    public CommonResult<List<BizStaffRespVO>> getStaffList(@RequestParam("ids") Collection<String> ids) {
        List<BizStaffDO> list = staffService.getStaffList(ids);
        return success(BizStaffConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/listAll")
    @ApiOperation("获得全部员工列表")
    @ApiImplicitParam(name = "ids", value = "编号列表", required = true, example = "1024,2048", dataTypeClass = List.class)
    @PreAuthorize("@ss.hasPermission('biz:staff:query')")
    public CommonResult<List<BizStaffRespVO>> getStaffListAll() {
        List<BizStaffDO> list = staffService.getStaffListAll();
        return success(BizStaffConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/page")
    @ApiOperation("获得员工分页")
    @PreAuthorize("@ss.hasPermission('biz:staff:query')")
    public CommonResult<PageResult<BizStaffRespVO>> getStaffPage(@Valid BizStaffPageReqVO pageVO) {
        PageResult<BizStaffDO> pageResult = staffService.getStaffPage(pageVO);
        return success(BizStaffConvert.INSTANCE.convertPage(pageResult));
    }

    @GetMapping("/export-excel")
    @ApiOperation("导出员工 Excel")
    @PreAuthorize("@ss.hasPermission('biz:staff:export')")
    @OperateLog(type = EXPORT)
    public void exportStaffExcel(@Valid BizStaffExportReqVO exportReqVO,
              HttpServletResponse response) throws IOException {
        List<BizStaffDO> list = staffService.getStaffList(exportReqVO);
        // 导出 Excel
        List<BizStaffExcelVO> datas = BizStaffConvert.INSTANCE.convertList02(list);
        ExcelUtils.write(response, "员工.xls", "数据", BizStaffExcelVO.class, datas);
    }

}
